package org.korifey.ffalg;

import java.io.Serializable;
import java.util.BitSet;

public class FFBitSet implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4857979165670395249L;
	
	public static final int SHIFT = 5;
	public static final int CHUNKSIZE = 1 << SHIFT;
	public static final int CHUNKMASK = CHUNKSIZE - 1;	
	private final int size;	
	private final int[] chunks;	
	
	public FFBitSet(final int size) {
		assert size >= 0;
		this.size = size;
		chunks = new int[(size+CHUNKSIZE-1)/CHUNKSIZE];		
	}
		
	
	public final void set(final int bit) {
		assert bit >= 0 && bit < size;
		chunks[bit >> SHIFT] |= (1 << (bit & CHUNKMASK));
	}
	
	public final void unset(final int bit) {
		assert bit >= 0 && bit < size;
		chunks[bit >> SHIFT] &= -1 ^ (1 << (bit & CHUNKMASK));
	}
	
	public final boolean get(final int bit) {
		assert bit >= 0 && bit < size;
		return (chunks[bit >> SHIFT] & (1 << (bit & CHUNKMASK))) != 0;
	}
	
	public final void flip(final int bit) {
		assert bit >= 0 && bit < size;
		chunks[bit >> SHIFT] ^= (1 << (bit & CHUNKMASK));
	}


	public int getSize() {
		return size;
	}
	
	
}
